$(function () {
  gebumenduibi = 1;
  initgebumenduibiData();
  getgebumenduibiTable();
})

function initgebumenduibiData() {
  const username = localStorage.getItem("user");
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");

  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 62,
    chartNo: 3,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    let data = chartData[0].data
    getinitgebumenduibiDataChart(data); //全所项目收支累计情况
  });
}

function getinitgebumenduibiDataChart(data) {
  let xData = [];
  let xiangmushoukuan = []; //成本预算
  let chengbenzhichu = []; //实际
  data.forEach((item) => {
    xData.push(item.key01)
    xiangmushoukuan.push((item.key02 / 10000).toFixed(2));
    chengbenzhichu.push((item.key03 / 10000).toFixed(2));
  });
  let legendData = ["项目收款", "成本支出"];
  let Data = [{
      name: "项目收款",
      type: "bar",
      barWidth: "45",
      barGap: 0,
      itemStyle: {
        color: "#165DFF",
      },
      label: {
        show: true,
        position: 'top',
        distance: 10,
        color: '#1D2129',
        fontWeight: 'bold'
      },
      data: xiangmushoukuan,
    },
    {
      name: "成本支出",
      type: "bar",
      barWidth: "45",
      barGap: 0.25,
      itemStyle: {
        color: "#20C997",
      },
      label: {
        show: true,
        position: 'top',
        distance: 10,
        color: '#1D2129',
        fontWeight: 'bold'
      },
      data: chengbenzhichu,
    },
  ]
  have('gebumenduibi-chart') ? yueduqushi('gebumenduibi-chart', xData, legendData, Data) : ''
}

// let cols = [
//   [{
//       field: 'key0',
//       title: '指标',
//       width: 200,
//     },
//     {
//       field: 'key1',
//       title: '',
//       sort: true,
//       minWidth: 160,
//       align: 'right',
//     },
//     {
//       field: 'key2',
//       title: '',
//       sort: true,
//       minWidth: 160,
//       align: 'right',
//     },
//     {
//       field: 'key3',
//       title: '',
//       sort: true,
//       minWidth: 160,
//       align: 'right'
//     },
//     {
//       field: 'key4',
//       title: '',
//       sort: true,
//       minWidth: 160,
//       align: 'right'
//     },
//     {
//       field: 'key5',
//       title: '',
//       sort: true,
//       minWidth: 160,
//       align: 'right'
//     },
//     {
//       field: 'key6',
//       title: '',
//       sort: true,
//       minWidth: 160,
//       align: 'right'
//     },
//     {
//       field: 'key7',
//       title: '',
//       sort: true,
//       minWidth: 160,
//       align: 'right'
//     },
//     {
//       field: 'key8',
//       title: '合计',
//       width: 130,
//       sort: true,
//       // fixed: "right",
//       align: "right"
//     }
//   ]
// ];

// let cols = []; //动态的表头


function getgebumenduibiTable() {
  const username = localStorage.getItem("user");
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 62,
    chartNo: 3,
  };
  
  ApiGet("getdata", obj, function (e) {
	  
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    let data = chartData;
	let cols = [
	  []
	];
	let shuju1 = []
	let shuju2 = []
	let arr = []
	let title = ['指标']
	let length = ''
    // console.log(data,'数据')
    for (c = 0; c < data.length; c++) {
      length = data.length
      title.push(data[c].key01)
	  console.log(Number((data[c].key02) / 10000).toFixed(2))
      shuju1.push(Number((data[c].key02) / 10000).toFixed(2))
      shuju2.push(Number((data[c].key03) / 10000).toFixed(2))
    }

    // 表头对应数据
    for (i = 0; i < length + 1; i++) {
      let temp = {}
      temp.field = '',
        temp.title = '',
        temp.sort = true,
        temp.align = 'right';
      cols[0].push(temp)
    }
    console.log(cols, 'cols')
    let lastone = {
      field: '',
      title: '合计',
      sort: true,
      align: 'right',
    }
    cols[0].push(lastone)

    //动态渲染表头
    cols.forEach((item) => {
      // console.log(title)
      // console.log(data)
      // console.log(item)
      for (var i = 0; i < item.length; i++) {
        // console.log(item[i], '每一项')
        if (item[i].title == '') {
          item[i].title = title[i]
        }
        if (item[0]) {
          item[0].sort = false
          item[0].align = 'left'
          item[0].field = 'key0'
        }
        if (item[1]) {
          item[1].field = 'key1'
        }
        if (item[2]) {
          item[2].field = 'key2'
        }
        if (item[3]) {
          item[3].field = 'key3'
        }
        if (item[4]) {
          item[4].field = 'key4'
        }
        if (item[5]) {
          item[5].field = 'key5'
        }
        if (item[6]) {
          item[6].field = 'key6'
        }
        if (item[7]) {
          item[7].field = 'key7'
        }
        if (item[8]) {
          item[8].field = 'key8'
        }
        if (item[9]) {
          item[9].field = 'key9'
        }
      }
    })



    //总和
    shuju1.push(sum(shuju1).toFixed(2))
    shuju2.push(sum(shuju2).toFixed(2))
    //插入开头
    shuju1.unshift('项目收款')
    shuju2.unshift('成本支出')
    //纵向数据改为横向数据
    shuju1 = createKeys(shuju1)
    shuju2 = createKeys(shuju2)
    arr.push(shuju1)
    arr.push(shuju2)


    //动态渲染数据
    // cols[0].forEach((item) => {
    //   console.log(arr)
    //   // console.log(item)
    //   for (var i = 0; i < item.length; i++) {
    //     if (item[i].field == '') {
    //       console.log(item[i].field, '每一项')
    //       item[0].field = 'key1'
    //     }
    //   }
    // })


    tableDrawing(arr);

    function tableDrawing(data) {
      console.log(data, 'ddddd')
      layui.use('table', function () {
        let table = layui.table;
        table.render({
          elem: '#newsList',
          cols: cols,
          data: data, // 数据
          // limit: limit, // 每页显示的条数
          // height: tableHeight,
        });
      })
    }
  });
}


function formatFigure(date) {
  let str = '';
  let num = '';
  let newStr = '';
  date = date + '';
  if (date.indexOf('.') != -1) {
    num = date.split('.')[0]
    str = date.split('.')[1]
  } else {
    num = date
  }
  newStr = num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
  newStr = str == '' ? newStr : newStr + '.' + str;
  return newStr;
}

function createKeys(arr) {
  const obj = {};
  arr.forEach((value, index) => {
    obj['key' + index] = value;
  })
  return obj;
}

function sum(arr) {
  let all = 0;
  arr.forEach((item) => {
    item = Number(item)
    all += item
  })
  return all;
}